

App({

	onHide: function() {

	},
	onShow: function() {
     this.getUserInfo('1');
      wx.showShareMenu({
        withShareTicket: true,
        menus: ['shareAppMessage', 'shareTimeline']
      })
	},
	getUserInfo:function(type){

	let promise = new Promise(function (resolve, reject) {
    wx.login({
	     success (res) {
			  if (res.code) {
		        wx.request({
		            url: getApp().globalData.baseUrl + '/wx/auth',
		            method:"POST",
		            header: {
						'code':res.code, //公用请求头
						'content-type': 'application/json'
					},
		            success: function(res) {
		              console.log(res.data)
		               if (res.data.code==200) {
		               	if (!res.data.obj.nickName&&type=='1') {
		                   
		               	}
		               	var userInfo= res.data.obj;
		               	 wx.setStorageSync('userInfo', res.data.obj)
		               	if (!type) {
		               		 resolve(res.data.obj);
                              return;
		               	}
		                 
		                  wx.getLocation({  //获取当前地址
					      type: 'gcj02',
					      success: function (res){
					        var latitude = res.latitude // 纬度，浮点数，范围为90 ~ -90
					        var longitude = res.longitude // 经度，浮点数，范围为180 ~ -180
					        wx.request({
				            url:getApp().globalData.baseUrl+'/area/baid/analysis/atong?lat='+latitude+'&lng='+longitude,
				            method:"GET",
				            header: {
				                'openid':userInfo.openid, //公用请求头
				                'content-type': 'application/json'
				            },
				            success: function(res) {
				             console.log(res.data.obj)
				              wx.setStorageSync('address', res.data.obj);

							    if (userInfo.city&&userInfo.city!='') {
							    	return
							    } else{
							    	userInfo.city= res.data.obj.city;
							    	userInfo.province= res.data.obj.province;
							    }
							    console.log('更新用户信息')
							    var _self=this;
							     wx.request({
							            url:getApp().globalData.baseUrl+'/wx/user/update',
							            method:"post",
							            header: {
							                'openid':userInfo.openid, //公用请求头
							                'content-type': 'application/json'
							            },
							            data:userInfo,
							            success: function(res) {
							            if(res.data.code==200){
							               wx.setStorageSync('userInfo',userInfo);
							               _self.triggerEvent('loginSuccess')
							            }  
							         }
							       })
					         }
					       })
					      }
					    })

		               	 resolve(res.data.obj);
		               	
		              }
		            }
		        })
			    } 
			    else {
			      console.log('登录失败！' + res.errMsg)
			    }
			  }
		})

    })
    return promise;

	},
	globalData: {
		baseUrl: 'https://tata.shebeib.com',
		//baseUrl: 'https://tatatest.shebeib.com',

		header: {
			'token':'', //公用请求头
			'content-type': 'application/json'
		},
		userInfo: null,
		newUser:null,
		
	}
})